package org.xprinter;

import android.content.Context;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.PrintInfoBean;
import com.example.sunmiv1.utils.LogUtil;

import net.posprinter.IConnectListener;
import net.posprinter.IDeviceConnection;
import net.posprinter.POSConnect;
import net.posprinter.TSPLConst;
import net.posprinter.TSPLPrinter;

import org.application.ClientApplication;

import java.util.List;

/***
 *
 * Xprinter 珠海芯烨 标签打印SDK
 *
 *  xp-235B 标签打印机 SDK来自珠海芯烨 官网Android SDK
 *
 *  https://www.xprinter.net/companyfile/1/
 *
 *  芯烨条码/热转印系列产品SDK安卓开发包
 *  适用于Xprinter芯烨4寸电子面单条码系列、热转印系列产品
 *
 */
public class XprinterManager {

    private static final String TAG = XprinterManager.class.getSimpleName();
    private static XprinterManager instance;

    private XprinterManager() {
    }

    public static XprinterManager getInstance() {
        if (instance == null) {
            instance = new XprinterManager();
        }
        return instance;
    }

    public void initXprinterSdk(Context context) {
        POSConnect.init(context);
        List<String> usbNames = POSConnect.getUsbDevices(context);
        if (!usbNames.isEmpty()) {
            onConnectUsbDevice(usbNames.get(0));
        }
    }


    IDeviceConnection deviceConn;
    TSPLPrinter tsplPrinter;

    public void onConnectUsbDevice(String usbPath) {
        if (deviceConn != null) {
            deviceConn.close();
        }
        LogUtil.i(TAG, "onConnectUsbDevice usbPath :" + usbPath);
        deviceConn = POSConnect.createDevice(POSConnect.DEVICE_TYPE_USB);
        deviceConn.connect(usbPath, connectListener);
    }

    private IConnectListener connectListener = (code, connInfo, msg) -> {
        LogUtil.i(TAG, "IConnectListener connInfo :" + connInfo + ". msg:" + msg);
        if (code == POSConnect.CONNECT_SUCCESS) {
            LogUtil.i(TAG, "");
            tsplPrinter = new TSPLPrinter(deviceConn);
            // printContent();
        } else if (code == POSConnect.CONNECT_FAIL) {

        }
    };

    //1mm =8 dots 364
    // 60 480 30 240
    //
    public void printPayReceipt(PrintInfoBean printInfoBean) {
        if (tsplPrinter == null) {
            Toast.makeText(ClientApplication.context, R.string.result_prn_unknown_err, Toast.LENGTH_LONG).show();
            return;
        }
        String billNo = "";
        Integer no = printInfoBean.getData().getPrint_number();
        if (no != null) {
            billNo = buildBillNo(no.toString());
        }
        String orderNo=printInfoBean.getData().getOrder_no();
        if (billNo.isEmpty()) {
            String order_no = printInfoBean.getData().getOrder_no();
            billNo = order_no.substring(order_no.length() - 4, order_no.length());
        }
        List<PrintInfoBean.DataBean.GoodsBean> goods = printInfoBean.getData().getGoods();
        int size = goods.size();
        int width = 40 * 8;
        int height = 30 * 8;
        String oneText = "編號" + billNo;
        int showSize = size;

        for (int i = 0; i < size; i++) {
            PrintInfoBean.DataBean.GoodsBean goodsBean = goods.get(i);

            if (goodsBean.getNum() > 1) {
                showSize += (goodsBean.getNum() - 1);
            }
        }
        int showIndex = 0;
        for (int i = 0; i < size; i++) {
            PrintInfoBean.DataBean.GoodsBean goodsBean = goods.get(i);

            double gNum = goodsBean.getNum();

            String goodsName = goodsBean.getGoods_name();
            String sku_name = goodsBean.getSku_name();
            if (sku_name == null || sku_name.isEmpty()) {
                //sku_name = "加珍珠，少冰，半糖，加巧克力";
            }
            String allMoney = "$" + goodsBean.getPrice();
            if (gNum > 1) {
                for (int j = 0; j < gNum; j++) {
                    showIndex += 1;
                    String sizeNo = showIndex + "/" + showSize;
                    printTask(width, sizeNo, oneText, goodsName, sku_name, allMoney);
                }
            } else {
                showIndex += 1;
                String sizeNo = showIndex + "/" + showSize;
                printTask(width, sizeNo, oneText, goodsName, sku_name, allMoney);
            }
        }
        // printText();
    }

    int limit = 12;

    void printTask(int width, String sizeNo, String oneText, String goodsName, String sku_name, String allMoney) {
        LogUtil.i(TAG, "sizeNo :" + sizeNo + " oneText: " + oneText + " goodsName:" + goodsName + " sku_name :" + sku_name + " allMoney:" + allMoney);
        if (sku_name.length() > limit) {
            String other0 = sku_name.substring(0, limit);
            String other = sku_name.substring(limit, sku_name.length());
            tsplPrinter.sizeMm(40.0, 30.0)
                    .density(10)
                    .reference(0, 0)
                    .direction(TSPLConst.DIRECTION_REVERSE, false)
                    .cls()
                    .text(10, 30, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, oneText)
                    .text(width - 18 - sizeNo.length() * 12, 30, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, sizeNo)
                    .text(10, 70, TSPLConst.FNT_SIMPLIFIED_CHINESE, 2, 2, goodsName)
                    .text(10, 130, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, other0)
                    .text(10, 160, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, other)
                    .text(width - 18 - allMoney.length() * 12, 190, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, allMoney)
                    .print();
        } else {
            tsplPrinter.sizeMm(40.0, 30.0)
                    .density(10)
                    .reference(0, 0)
                    .direction(TSPLConst.DIRECTION_REVERSE, false)
                    .cls()
                    .text(10, 30, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, oneText)
                    .text(width - 18 - sizeNo.length() * 12, 30, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, sizeNo)
                    .text(10, 70, TSPLConst.FNT_SIMPLIFIED_CHINESE, 2, 2, goodsName)
                    .text(10, 130, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, sku_name)
                    .text(width - 18 - allMoney.length() * 12, 176, TSPLConst.FNT_SIMPLIFIED_CHINESE, 1, 1, allMoney)
                    .print();
        }

    }

    String buildBillNo(String no) {
        if (no.length() < 4) {
            return buildBillNo("0".concat(no));
        } else {
            return no;
        }
    }


}
